package com.bjsxt.investor.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.util.BasicService;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.investor.dao.IAInvestorDao;
import com.bjsxt.investor.dao.IBankDao;
import com.bjsxt.investor.dao.ICateDao;
import com.bjsxt.investor.dao.IGrantDao;
import com.bjsxt.investor.dao.IInvestorBankDao;
import com.bjsxt.investor.dao.IInvestorCateDao;
import com.bjsxt.investor.pojo.ABank;
import com.bjsxt.investor.pojo.ACate;
import com.bjsxt.investor.pojo.AGrant;
import com.bjsxt.investor.pojo.AInvestor;
import com.bjsxt.investor.pojo.AInvestorBank;
import com.bjsxt.investor.pojo.AInvestorCate;
import com.bjsxt.investor.service.IInvestorService;

/**
 * 所有有关出资方的servcie的实现
 * 
 * @author Administrator
 *
 */
@Service("investorService")
public class InvestorServiceImpl extends BasicService implements IInvestorService
{
	@Resource
	private IAInvestorDao investorDao;
	@Resource
	private IBankDao bankDao;
	@Resource
	private IGrantDao grantDao;
	@Resource
	private ICateDao cateDao;
	@Resource
	private IInvestorBankDao investorBankDao;
	@Resource
	private IInvestorCateDao investorCateDao;

	/* =================出资方的service的实现开始==================== */
	@Override
	public JSONObject insertInvestorService(AInvestor investor)
	{
		JSONObject resultJson = new JSONObject();

		int insert = this.investorDao.insert(investor);

		if (insert > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "出资方添加成功");
			JSONObject data = new JSONObject();
			data.put("id", investor.getId());
			resultJson.put("data", data);
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "出资方添加失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteInvestorService(Map<String, Object> condMap)
	{
		JSONObject resultJson = new JSONObject();
		int delete = this.investorDao.delete(condMap);
		if (delete > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "出资方删除成功");
			JSONObject data = new JSONObject();
			data.put("删除个数", delete);
			resultJson.put("data", data);
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "出资方删除失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateInvestorService(AInvestor investor, Map<String, Object> condMap)
	{
		JSONObject resultJson = new JSONObject();
		int update = this.investorDao.update(investor);
		if (update > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "出资方修改成功");
			JSONObject data = new JSONObject();
			data.put("id", investor.getId());
			resultJson.put("data", data);
			Object TempFile = condMap.get("file");
			if (TempFile != null)
			{

				/* 开始文件上传 */
				MultipartFile multipartFile = (MultipartFile) TempFile;
				String fileName = multipartFile.getOriginalFilename();
				String rootPath = condMap.get("rootPath") + "";
				String webPath = "/upload/investor/" + this.dateFormatUtil.formatDate(new Date(), "yyyy-MM-dd") + "/"
						+ UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."),fileName.length());
				String realPath = rootPath + webPath;
				/* 文件拷贝 */
				File file = new File(realPath);
				if (!file.getParentFile().exists())
				{
					file.getParentFile().mkdirs();
				}
				try
				{
					/* 头像上传成功 */
					if (this.fileCopyUtil.copyFile(multipartFile.getInputStream(), new FileOutputStream(file)))
					{
						/* 删除原来的头像 */
						File oldFile = new File(rootPath+investor.getLogo());
						if(oldFile.exists())
						{
							if(!oldFile.delete())
							{
								resultJson.put("info", "头像删出失败上传失败,出资方ID："+investor.getId()+"图片路径："+investor.getLogo());
							}
						}
						investor.setLogo(webPath);
						this.investorDao.update(investor);
						
					} else
					{
						resultJson.put("info", "头像上传失败");
					}
					;
				} catch (FileNotFoundException e)
				{
					e.printStackTrace();
				} catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "出资方修改失败");
		}
		return resultJson;
	}

	@Override
	public AInvestor findOneInvestorService(Map<String, Object> condMap)
	{

		return this.investorDao.findOne(condMap);
	}

	@Override
	public List<AInvestor> findListInvestorService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 修改条件 */
		if (condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		if (pageInfoUtil == null)
		{
			/* 不分页查询 */
			return this.investorDao.findList(condMap);
		} else
		{
			/* 分页查询 */
			/* 先查总记录数 */
			condMap.put("pageOperation", "true");
			List<AInvestor> findList = this.investorDao.findList(condMap);
			pageInfoUtil.setTotalRecord(findList.get(0).getId());
			/* 查询总记录数 */
			condMap.put("pageOperation", "false");
			condMap.put("currRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			List<AInvestor> list = this.investorDao.findList(condMap);
			return list;
		}
	}
	/* =================出资方的service的实现结束==================== */
	/* =================银行的service的实现开始==================== */

	@Override
	public JSONObject insertBankService(ABank Bank)
	{
		JSONObject resultJson = new JSONObject();

		int insert = this.bankDao.insert(Bank);

		if (insert > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "银行添加成功");
			JSONObject data = new JSONObject();
			data.put("id", Bank.getId());
			resultJson.put("data", data);
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "银行添加失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteBankService(Map<String, Object> condMap)
	{
		JSONObject resultJson = new JSONObject();
		int delete = this.bankDao.delete(condMap);
		if (delete > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "银行删除成功");
			JSONObject data = new JSONObject();
			data.put("删除个数", delete);
			resultJson.put("data", data);
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "银行删除失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateBankService(ABank Bank)
	{
		JSONObject resultJson = new JSONObject();
		int update = this.bankDao.update(Bank);

		if (update > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "银行修改成功");
			JSONObject data = new JSONObject();
			data.put("id", Bank.getId());
			resultJson.put("data", data);
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "银行修改失败");
		}
		return resultJson;
	}

	@Override
	public ABank findOneBankService(Map<String, Object> condMap)
	{

		return this.bankDao.findOne(condMap);
	}

	@Override
	public List<ABank> findListBankService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 修改条件 */
		if (condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		if (pageInfoUtil == null)
		{
			/* 不分页查询 */
			return this.bankDao.findList(condMap);
		} else
		{
			/* 分页查询 */
			/* 先查总记录数 */
			condMap.put("pageOperation", "true");
			List<ABank> findList = this.bankDao.findList(condMap);
			pageInfoUtil.setTotalRecord(findList.get(0).getId());
			/* 查询总记录数 */
			condMap.put("pageOperation", "false");
			condMap.put("currRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			List<ABank> list = this.bankDao.findList(condMap);
			return list;
		}
	}
	/* =================银行的service的实现结束==================== */

	/* =================权限的service的实现开始==================== */

	@Override
	public JSONObject insertGrantService(AGrant Grant)
	{
		JSONObject resultJson = new JSONObject();

		int insert = this.grantDao.insert(Grant);

		if (insert > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "银行添加成功");
			JSONObject data = new JSONObject();
			data.put("id", Grant.getId());
			resultJson.put("data", data);
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "银行添加失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteGrantService(Map<String, Object> condMap)
	{
		JSONObject resultJson = new JSONObject();
		int delete = this.grantDao.delete(condMap);
		if (delete > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "银行删除成功");
			JSONObject data = new JSONObject();
			data.put("删除个数", delete);
			resultJson.put("data", data);
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "银行删除失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateGrantService(AGrant Grant)
	{
		JSONObject resultJson = new JSONObject();
		int update = this.grantDao.update(Grant);

		if (update > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "银行修改成功");
			JSONObject data = new JSONObject();
			data.put("id", Grant.getId());
			resultJson.put("data", data);
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "银行修改失败");
		}
		return resultJson;
	}

	@Override
	public AGrant findOneGrantService(Map<String, Object> condMap)
	{

		return this.grantDao.findOne(condMap);
	}

	@Override
	public List<AGrant> findListGrantService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 修改条件 */
		if (condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		if (pageInfoUtil == null)
		{
			/* 不分页查询 */
			return this.grantDao.findList(condMap);
		} else
		{
			/* 分页查询 */
			/* 先查总记录数 */
			condMap.put("pageOperation", "true");
			List<AGrant> findList = this.grantDao.findList(condMap);
			pageInfoUtil.setTotalRecord(findList.get(0).getId());
			/* 查询总记录数 */
			condMap.put("pageOperation", "false");
			condMap.put("currRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			List<AGrant> list = this.grantDao.findList(condMap);
			return list;
		}
	}
	/* =================权限的service的实现结束==================== */

	/* =================分类的service的实现开始==================== */

	@Override
	public JSONObject insertCateService(ACate Cate)
	{
		JSONObject resultJson = new JSONObject();

		int insert = this.cateDao.insert(Cate);

		if (insert > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "银行添加成功");
			JSONObject data = new JSONObject();
			data.put("id", Cate.getId());
			resultJson.put("data", data);
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "银行添加失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteCateService(Map<String, Object> condMap)
	{
		JSONObject resultJson = new JSONObject();
		int delete = this.cateDao.delete(condMap);
		if (delete > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "银行删除成功");
			JSONObject data = new JSONObject();
			data.put("删除个数", delete);
			resultJson.put("data", data);
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "银行删除失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateCateService(ACate Cate)
	{
		JSONObject resultJson = new JSONObject();
		int update = this.cateDao.update(Cate);

		if (update > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "银行修改成功");
			JSONObject data = new JSONObject();
			data.put("id", Cate.getId());
			resultJson.put("data", data);
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "银行修改失败");
		}
		return resultJson;
	}

	@Override
	public ACate findOneCateService(Map<String, Object> condMap)
	{

		return this.cateDao.findOne(condMap);
	}

	@Override
	public List<ACate> findListCateService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 修改条件 */
		if (condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		if (pageInfoUtil == null)
		{
			/* 不分页查询 */
			return this.cateDao.findList(condMap);
		} else
		{
			/* 分页查询 */
			/* 先查总记录数 */
			condMap.put("pageOperation", "true");
			List<ACate> findList = this.cateDao.findList(condMap);
			pageInfoUtil.setTotalRecord(findList.get(0).getId());
			/* 查询总记录数 */
			condMap.put("pageOperation", "false");
			condMap.put("currRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			List<ACate> list = this.cateDao.findList(condMap);
			return list;
		}
	}
	/* =================分类的service的实现结束==================== */

	/* =================出资方和银行的service的实现开始==================== */

	@Override
	public JSONObject insertInvestorBankService(AInvestorBank InvestorBank)
	{
		JSONObject resultJson = new JSONObject();
		Map<String, Object> condMap = new HashMap<String, Object>();;
		
		condMap.put("investorId", InvestorBank.getInvestorId());
		condMap.put("bankId", InvestorBank.getBankId());
		/* 查询数据库是否存在 */
		AInvestorBank temp = this.findOneInvestorBankService(condMap );
		if(temp != null)
		{
			resultJson.put("code", "2");
			resultJson.put("info", "出资方和银行已存在");
			return resultJson;
		}
		

		int insert = this.investorBankDao.insert(InvestorBank);

		if (insert > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "出资方和银行添加成功");
			JSONObject data = new JSONObject();
			data.put("id", InvestorBank.getId());
			resultJson.put("data", data);
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "出资方和银行添加失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteInvestorBankService(Map<String, Object> condMap)
	{
		JSONObject resultJson = new JSONObject();
		int delete = this.investorBankDao.delete(condMap);
		if (delete > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "出资方和银行删除成功");
			JSONObject data = new JSONObject();
			data.put("删除个数", delete);
			resultJson.put("data", data);
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "出资方和银行删除失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateInvestorBankService(AInvestorBank InvestorBank)
	{
		JSONObject resultJson = new JSONObject();
		int update = this.investorBankDao.update(InvestorBank);

		if (update > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "出资方和银行修改成功");
			JSONObject data = new JSONObject();
			data.put("id", InvestorBank.getId());
			resultJson.put("data", data);
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "出资方和银行修改失败");
		}
		return resultJson;
	}

	@Override
	public AInvestorBank findOneInvestorBankService(Map<String, Object> condMap)
	{
		return this.investorBankDao.findOne(condMap);
	}

	@Override
	public List<AInvestorBank> findListInvestorBankService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 修改条件 */
		if (condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		if (pageInfoUtil == null)
		{
			/* 不分页查询 */
			return this.investorBankDao.findList(condMap);
		} else
		{
			/* 分页查询 */
			/* 先查总记录数 */
			condMap.put("pageOperation", "true");
			List<AInvestorBank> findList = this.investorBankDao.findList(condMap);
			pageInfoUtil.setTotalRecord(findList.get(0).getId());
			/* 查询总记录数 */
			condMap.put("pageOperation", "false");
			condMap.put("currRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			List<AInvestorBank> list = this.investorBankDao.findList(condMap);
			return list;
		}
	}
	/* =================出资方和银行的service的实现结束==================== */

	/* =================出资方和权限的service的实现开始==================== */

	@Override
	public JSONObject insertInvestorCateService(AInvestorCate InvestorCate)
	{
		JSONObject resultJson = new JSONObject();
		
		/* 查重 */
		Map<String,Object> condMap = new HashMap<String,Object>();
		condMap.put("cateId", InvestorCate.getCateId());
		condMap.put("investorId", InvestorCate.getInvestorId());
		AInvestorCate temp = this.findOneInvestorCateService(condMap);
		
		if(temp != null)
		{
			resultJson.put("code", "2");
			resultJson.put("info", "出资方和权限已存在");
			return resultJson;
		}
		
		
		int insert = this.investorCateDao.insert(InvestorCate);

		if (insert > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "出资方和权限添加成功");
			JSONObject data = new JSONObject();
			data.put("id", InvestorCate.getId());
			resultJson.put("data", data);
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "出资方和权限添加失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteInvestorCateService(Map<String, Object> condMap)
	{
		JSONObject resultJson = new JSONObject();
		int delete = this.investorCateDao.delete(condMap);
		if (delete > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "出资方和权限删除成功");
			JSONObject data = new JSONObject();
			data.put("删除个数", delete);
			resultJson.put("data", data);
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "出资方和权限删除失败");
		}
		return resultJson;
	}

	@Override		
	public JSONObject updateInvestorCateService(AInvestorCate InvestorCate)
	{
		JSONObject resultJson = new JSONObject();
		int update = this.investorCateDao.update(InvestorCate);

		if (update > 0)
		{
			resultJson.put("code", "1");
			resultJson.put("info", "出资方和权限修改成功");
			JSONObject data = new JSONObject();
			data.put("id", InvestorCate.getId());
			resultJson.put("data", data);
		} else
		{
			resultJson.put("code", "0");
			resultJson.put("info", "出资方和权限修改失败");
		}
		return resultJson;
	}

	@Override
	public AInvestorCate findOneInvestorCateService(Map<String, Object> condMap)
	{
		return this.investorCateDao.findOne(condMap);
	}

	@Override
	public List<AInvestorCate> findListInvestorCateService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 修改条件 */
		if (condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		if (pageInfoUtil == null)
		{
			/* 不分页查询 */
			return this.investorCateDao.findList(condMap);
		} else
		{
			/* 分页查询 */
			/* 先查总记录数 */
			condMap.put("pageOperation", "true");
			List<AInvestorCate> findList = this.investorCateDao.findList(condMap);
			pageInfoUtil.setTotalRecord(findList.get(0).getId());
			/* 查询总记录数 */
			condMap.put("pageOperation", "false");
			condMap.put("currRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			List<AInvestorCate> list = this.investorCateDao.findList(condMap);
			return list;
		}
	}
	/* =================出资方和权限的service的实现结束==================== */
}
